package com.app.micaihu.view.login.dialog

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import com.app.base.BaseDialog
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.utils.ImageLoadUtils
import com.app.micaihu.view.bean.Military
import com.app.micaihu.view.main.dialog.adapter.MilitaryAdapter
import com.app.utils.biz.Biz
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.mian_no_login_dialog.*

/**
 * 更换 军种
 */
class ChangeMilitaryDialog(context: Context) : BaseDialog(context) {

    override fun getLayoutId() = R.layout.mian_no_login_dialog
    private val militaryAdapter: MilitaryAdapter by lazy {
        val militaryAdapter = MilitaryAdapter()
        val gridLayoutManager = GridLayoutManager(context, 3)
        rvMilitary.layoutManager = gridLayoutManager
        rvMilitary.adapter = militaryAdapter
        val list = initData()
        militaryAdapter.setNewInstance(list)
        militaryAdapter
    }

    override fun setData() {
        ImageLoadUtils.loadImageNoScaleType(R.mipmap.main_login_huniu, ivHuniu)
    }

    override fun addListeners() {
        tvOk.setOnClickListener {
            dismiss()
        }
        ivClose.setOnClickListener { dismiss() }
        militaryAdapter.setOnItemClickListener { adapter, view, position ->
            val item = militaryAdapter.getItem(position)
            militaryAdapter.setCheck(position)
            Biz.instance.military = item.id.toString()
            onChangeMilitaryListener?.changeMilitary(item.id)
        }
    }

    private fun initData(): ArrayList<Military> {
        val list = arrayListOf<Military>()
        val military = Biz.instance.military
        var militaryIndex = 1
        if (military.isNotEmpty()) {
            militaryIndex = military.toInt()
        }
        for ((index, item) in Config.militaryList.withIndex()) {
            val military: Military
            if (militaryIndex == index + 1) {
                military = Military(index + 1, item, Config.militaryIconList[index], true)
                Biz.instance.military = (index + 1).toString()
            } else {
                military = Military(index + 1, item, Config.militaryIconList[index], false)
            }
            list.add(military)
        }

        return list
    }

    var onChangeMilitaryListener: OnChangeMilitaryListener? = null

    interface OnChangeMilitaryListener {
        fun changeMilitary(index: Int)
    }

    fun setChangeMilitaryListener(onChangeMilitaryListener: OnChangeMilitaryListener) {
        this.onChangeMilitaryListener = onChangeMilitaryListener
    }

}